#include "../../include/ManageStudent.h"

using namespace std;


StudentController* stuController;
void manageStudent(StudentController* _stuController) {
    IdManager* idManager = IdManager::getIdManager();
    stuController = _stuController;
    string cmd;
    while (cmd != E_X_I_T ) {
        cmd = "";
        cout << "======学生管理主界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. createStudent 创建学生" << endl;
        cout << "2. listStudent   列出所有学生ID" << endl;
        cout << "3. editStudent   编辑学生" << endl;
        cout << "4. deleteStudent 删除学生" << endl;
        cout << "5. storeData 保存学生数据" << endl;
        cout << "6. loadData  加载学生数据" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == CREATE_STU) {
            createStudent();
        }
        else if(cmd == "2" || cmd == LIST_STU) {
            listStudent();
        }
        else if(cmd == "3" || cmd == EDIT_STU) {
            editStudent();
        }
        else if(cmd == "4" || cmd == DELETE_STU) {
            deleteStudent();
        }
        else if(cmd == "5" || cmd == STORE_DATA) {
            storeStudentData();
        }
        else if(cmd == "6" || cmd == LOAD_DATA) {
            loadStudentData();
        }
        cout << "=======================" << endl;
    }
    return;
}

Student* getStudentByNum() {
    int length = stuController->getStudentCount();
    Student* studentList[length];
    stuController->listAllStudent(studentList);
    cout << "序号 " << " 学号 " << " 姓名 " << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << ". " << studentList[i]->getId() << " " << studentList[i]->getName() << endl;
    }
    int studentNum = -1;
    while(true) {
        cout << "请输入学生序号:";
        cin >> studentNum;
        cout << endl;
        studentNum -= 1;
        if(studentNum >= 0 && studentNum < length) {
            break;
        }
        cout << "序号有误" << endl;
    }
    return studentList[studentNum];
}
void createStudent() {
    cout << "请输入学生姓名:";
    string name;
    cin >> name;
    stuController->createStudent(name);
    cout << name << "学生创建成功!" << endl;
}
void listStudent() {
    int length = stuController->getStudentCount();
    Student* studentList[length];
    stuController->listAllStudent(studentList);
    cout << "序号 " << " 学号 " << " 姓名 " << endl;
    for(int i=0;i<length;i++) {
        cout << i+1 << ". " << studentList[i]->getId() << " " << studentList[i]->getName() << endl;
    }
}
void editStudent() {
    Student* student = getStudentByNum();
    if(student == nullptr) return;
    string cmd;
    while(cmd != E_X_I_T) {
        cmd = "";
        cout << "======"<< student->getId() << "学生编辑界面=========" << endl;
        cout << "指令说明:" << endl;
        cout << "1. editName 编辑姓名" << endl;
        cin >> cmd;
        if(cmd == "1" || cmd == EDIT_NAME) {
            editName(student);
        }
    }
    cout << student->getId() << "学生编辑完成。" << endl;
}
void deleteStudent() {
    Student* student = getStudentByNum();
    string str = student->toString();
    if(student == nullptr) return;
    stuController->deleteStudent(student);
    cout << str << "删除成功" << endl;
}
void editName(Student* student) {
    cout << "请输入新名字:";
    string oldName = student->getName();
    string newName;
    cin >> newName;
    student->setName(newName);
    cout << student->getId() <<"学生姓名由 " << oldName << " 更改为 " << newName << endl; 
}
void storeStudentData() {
    int length = stuController->getStudentCount();
    Student* studentList[length];
    stuController->listAllStudent(studentList);

    ofstream fout(StudentDataPath);
    cout << "序号 " << " 学号 " << " 姓名 " << endl;
    for(int i=0;i<length;i++) {
        fout << studentList[i]->getId() << " " << studentList[i]->getName() << endl;
        cout << i << ". " << studentList[i]->getId() << " " << studentList[i]->getName() << endl;
    }
    fout.close();
    cout << "学生数据保存成功!" << endl;
}
void loadStudentData() {
    if(stuController->getStudentCount() != 0) {
        cout << "当前学生数量不为0,为避免冲突,请勿直接导入数据。" << endl;
        return;
    }
    ifstream fin(StudentDataPath);
    string line;
    while(getline(fin,line)) {
        istringstream iss(line);
        stuController->strToStudent(iss);
    }
    cout << "学生数据加载成功!" << endl;
}
